<template>
    <!-- 班级开课 -->

    <el-dialog title="选择用户" :visible.sync="visible" width="800px" top="5vh" append-to-body>
        <h4 class="form-header h4">选择班级开课</h4>
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
            <el-form-item label="班级名称" prop="name">
                <el-input v-model="queryParams.name" placeholder="请输入班级名称" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="班级码" prop="clazzCode">
                <el-input v-model="queryParams.clazzCode" placeholder="请输入手机号码" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <el-row>
            <el-table @row-click="clickRow" ref="table" :data="clazzList" @selection-change="handleSelectionChange"
                height="260px">
                <el-table-column type="selection" width="55"></el-table-column>
                <el-table-column label="班级名称" prop="name" :show-overflow-tooltip="true" />
                <el-table-column label="班主任" prop="headTeacher" :show-overflow-tooltip="true" />
                <el-table-column label="班级吗" prop="clazzCode" :show-overflow-tooltip="true" />
                <el-table-column label="状态" align="center" prop="status">
                    <template slot-scope="scope">
                        <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status" />
                    </template>
                </el-table-column>
                <el-table-column label="创建时间" align="center" prop="createTime" width="180">
                    <template slot-scope="scope">
                        <span>{{ parseTime(scope.row.createTime) }}</span>
                    </template>
                </el-table-column>
            </el-table>
            <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList" />
        </el-row>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="handleSelectClazz">确 定</el-button>
            <el-button @click="visible = false">取 消</el-button>
        </div>

    </el-dialog>
</template>
  
<script>
import { unallocatedClazzList, clazzSelectAll } from "@/api/feedback/course";

export default {
    dicts: ['sys_normal_disable'],
    props: {
        // 角色编号
        courseId: {
            type: [Number, String]
        }
    },
    data() {
        return {
            // 遮罩层
            visible: false,
            // 选中数组值
            clazzIds: [],
            // 总条数
            total: 0,
            // 未授权用户数据
            clazzList: [],
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                courseId: this.courseId,
                name: undefined,
                clazzCode: undefined
            }
        };
    },
    // created() {
    //     const courseId = this.$route.params && this.$route.params.courseId;
    //     if (courseId) {
    //         this.queryParams.courseId = courseId;
    //         this.show();
    //     }
    // },

    methods: {
        // 显示弹框
        show() {
            this.queryParams.courseId = this.courseId;
            this.getList();
            this.visible = true;
        },
        clickRow(row) {
            this.$refs.table.toggleRowSelection(row);
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.clazzIds = selection.map(item => item.id);
        },
        // 查询表数据
        getList() {
            unallocatedClazzList(this.queryParams).then(res => {
                this.clazzList = res.rows;
                this.total = res.total;
            });
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        /** 选择授权用户操作 */
        handleSelectClazz() {
            const courseId = this.queryParams.courseId;
            console.log(courseId);
            const clazzIds = this.clazzIds.join(",");
            if (clazzIds == "") {
                this.$modal.msgError("请选择要分配的班级");
                return;
            }
            clazzSelectAll({ courseId: courseId, clazzIds: clazzIds }).then(res => {
                this.$modal.msgSuccess(res.msg);
                this.getList();
                if (res.code === 200) {
                    this.visible = false;
                    this.$emit("ok");
                }
            });
        }
    }
};
</script>
  